package com.wangyg.juc.ProducerConsumer.version2;

import java.util.List;

/**
 * Created by wangyg6 on 2018/7/11.
 */
public class Consumer implements Runnable {
	private List<String> list;

	public Consumer setList(List<String> list) {
		this.list = list;
		return this;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (list) {
				if (list.size() <= 0) {//空了
					try {
						list.notifyAll();
						list.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if (list.size() > 0)//唤醒时会出现为空却remove,空指针
					System.out.println(Thread.currentThread().getName() + "消费了" + list.remove(0));
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

